package com.meterreadingsys.activity.usermanager;


import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;

import com.meterreadingsys.activity.R;
import com.meterreadingsys.db.DBOpenHelper;
import com.meterreadingsys.db.UserInfo;
import com.meterreadingsys.db.UserInfoDB;
import com.meterreadingsys.util.AChartEngine;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class QueryUserInfoActivity extends Activity {
	public EditText name;

	public EditText phone;

	public EditText adds;

	public Button modify;

	public Button delect;

	public UserInfoDB db;
	
	public EditText id;
	
	public Button button;
	
	public SQLiteDatabase sqldb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.queryuserinfo);
		name = (EditText) findViewById(R.id.name);
		phone = (EditText) findViewById(R.id.phone);
		adds = (EditText) findViewById(R.id.adds);
		modify = (Button) findViewById(R.id.userinfomodify);
		delect = (Button) findViewById(R.id.userinfodelect);
		id=(EditText)findViewById(R.id.editText1);
		id.setVisibility(View.INVISIBLE);
		button=(Button)findViewById(R.id.test);
		DBOpenHelper helper=new DBOpenHelper(this);
		sqldb=helper.getWritableDatabase();
		db=new UserInfoDB(sqldb);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AChartEngine ace=new AChartEngine();
				
				String number=id.getEditableText().toString();
				  Intent intent = ChartFactory.getBarChartIntent(QueryUserInfoActivity.this, ace.getBarDataset(db.query(number)), ace.getBarRenderer(db.query(number)), Type.DEFAULT);
				  startActivity(intent);
			}
		});
		
		
		initUserinfo(0,"next");
		delect.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int userid=Integer.parseInt(id.getEditableText().toString());
			boolean b	=db.delectUserInfo(userid);
			if(b){
				 Toast.makeText(QueryUserInfoActivity.this, "删除成功", Toast.LENGTH_SHORT)
	             .show();
				 initUserinfo(userid,"next");
			}
                
			}
		});
		modify.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int userid=Integer.parseInt(id.getEditableText().toString());
				ContentValues value=new ContentValues();
				value.put("username", name.getEditableText().toString());
				value.put("useradds", adds.getEditableText().toString());
				value.put("phone", phone.getEditableText().toString());
				boolean b=db.modifyUserInfo(value, userid);
				if(b){
					 Toast.makeText(QueryUserInfoActivity.this, "修改成功", Toast.LENGTH_SHORT)
		             .show();
					 initUserinfo(userid,"modify");
				}
				
			}
		});
	}
	
	

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int userid=Integer.parseInt(id.getEditableText().toString());
		if(keyCode==20){
			initUserinfo(userid,"next");
			return true;
		}else if(keyCode==19){
			initUserinfo(userid,"no");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}



	public void initUserinfo(int userid,String tag){
		UserInfo user = new UserInfo();
		user.setId(userid);
		UserInfo u = db.queryUserInfo(user,tag);
		if(u!=null){
			name.setText(u.getName());
			phone.setText(u.getPhone());
			adds.setText(u.getAdds());
		
			id.setText(u.getId()+"");
		}else{
			Toast.makeText(QueryUserInfoActivity.this, "没了", Toast.LENGTH_SHORT)
            .show();
		}
		
	}



	@Override
	protected void onDestroy() {
		sqldb.close();
		super.onDestroy();
	}
      
	
	 
}
